package cn.com.elite;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ebao.gs.integration.sms.entity.SMSEntity;
import com.ebao.gs.integration.sms.module.SMS;
import com.ebao.gs.integration.sms.service.InternalSMSService;
import com.ebao.gs.integration.sms.sybase365.SMSSenderImpl;

public class SMSThread implements Runnable{
	private static final Log log=LogFactory.getLog(SMSThread.class);
	private boolean flag=true; 
	private static int Hz=5; 
	public static String times;
	public static MYBean mybean;
	public static DataSource dataSource1;
	public static DataSource dataSource2;
	private static String sql=" select * from Documentout where DOCTYPE=3 and ((Status=0) or (Status!=1 and TrialCount<TrialCountLimit)) "; 
	public static InternalSMSService service;
	public static SMSSenderImpl senderImpl;
	
	public static int getHz() {
		return Hz;
	}

	public static void setHz(int hz) {
		Hz = hz;
	}

	public static String getTimes() {
		return times;
	}

	public static void setTimes(String times) {
		SMSThread.times = times;
	}

	public static MYBean getMybean() {
		return mybean;
	}

	public static void setMybean(MYBean mybean) {
		SMSThread.mybean = mybean;
	}

	public static String getSql() {
		return sql;
	}

	public static void setSql(String sql) {
		SMSThread.sql = sql;
	}

	public static InternalSMSService getService() {
		return service;
	}

	public static void setService(InternalSMSService service) {
		SMSThread.service = service;
	}

	public static SMSSenderImpl getSenderImpl() {
		return senderImpl;
	}

	public static void setSenderImpl(SMSSenderImpl senderImpl) {
		SMSThread.senderImpl = senderImpl;
	}

	public static DataSource getDataSource1() {
		return dataSource1;
	}

	public static void setDataSource1(DataSource dataSource1) {
		SMSThread.dataSource1 = dataSource1;
	}

	public static DataSource getDataSource2() {
		return dataSource2;
	}

	public static void setDataSource2(DataSource dataSource2) {
		SMSThread.dataSource2 = dataSource2;
	}

	public void run(){
		log.info("---------------------MY短信接口----------------------------");
		try{
			log.info("times:"+times);
			Hz=Integer.parseInt(times);
		}catch(Exception e){
			log.error(Util.getERRO(e));
			log.error("因为获取运行频率出错，改为默认频率5秒钟运行");
		}
		while(flag){
			log.info("扫描documentout表中未处理的记录："+sql);
			List list1=mybean.chaxun_sql(sql,dataSource1);
			dorun(list1,dataSource1);
			List list2=mybean.chaxun_sql(sql,dataSource2);
			dorun(list2,dataSource2);
			try{
				Thread.sleep(Hz*1000); 
			}catch(Exception e){
				log.error(Util.getERRO(e));
			}
		}
	}

	private void dorun(List list,DataSource dataSource){
		try{
			if(list!=null){
				for(int i=0;i<list.size();i++){
					Map map=(Map)list.get(i); 
					String mobile=Util.filterForObject(map.get("DocumentTo"));
					log.info("mobile:"+mobile);
					String DocumentPath=Util.filterForObject(map.get("DocumentPath"));
					log.info("DocumentPath:"+DocumentPath);
					String TrialCountLimit=Util.filterForObject(map.get("TrialCountLimit"));
					log.info("TrialCountLimit:"+TrialCountLimit);
					String DocumentOut_GUID=Util.filterForObject(map.get("DocumentOut_GUID"));
					log.info("DocumentOut_GUID:"+DocumentOut_GUID);
					try{
						SMS sms=new SMS();
						sms.setCritical(true);// will be resend ,otherwise will not.
						sms.setEstimatedSendTime(new Date());
						sms.setReceivers(Arrays.asList(new String[]{mobile}));
						sms.setText(DocumentPath);
						Long[] smsIdArray = service.sendSMS(sms);
						for(int i1=0;i1<smsIdArray.length;i1++) {
							String s="SMS created id="+smsIdArray[i1];
							log.info(s);
						}		
						String update_sql=" update DocumentOut set Status=1,TrialCount=TrialCount+1,LastTryDateTime=sysdate,FinishDateTime=sysdate,ModifiedDate=sysdate where DocumentOut_GUID='"+DocumentOut_GUID+"'";
						mybean.update_sql(update_sql,dataSource);
						while(true){
				        	Collection<SMSEntity> smslist=service.findUnsendSMS(50);
				        	if(smslist==null||smslist.isEmpty()){
				        		break;
				        	}
				        	for(SMSEntity entity:smslist){
				        		try{
				        			List list1=senderImpl.sendSMS(entity.toSMS());
				        			log.info("list1:"+list1);
				        		}catch(Exception e){
				        			log.error(Util.getERRO(e));
				        		}
				        	}
				        }
					}catch(Exception e){
						log.error(Util.getERRO(e));
					}			
				}
			}
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}
	}
}